package com.zmn.plat.services.impl.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.user.UserCount;
import com.zmn.plat.model.entity.user.UserQuery;
import com.zmn.plat.persistence.interfaces.user.UserCountDao;
import com.zmn.plat.services.interfaces.user.UserCountService;

/**
 * @author nowind
 * @since 2017/7/31 14:34
 */
@Service
public class UserCountServiceImpl implements UserCountService {

    @Resource
    private UserCountDao userCountDao;

    @Override
    public UserCount findByKey(Integer userId) {
        return userCountDao.findByKey(userId);
    }

    @Override
    public List<UserCount> listPageByQuery(UserQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : userCountDao.listPageByQuery(query);
    }

    @Override
    public List<UserCount> listByQuery(UserQuery query) {
        return userCountDao.listByQuery(query);
    }

    @Override
    public Integer countByQuery(UserQuery query) {
        return userCountDao.countByQuery(query);
    }

    @Override
    public Integer updateBuyCount(Integer userId, Integer changes) {
        UserCount userCount = new UserCount();
        userCount.setUserId(userId);
        userCount.setBuyTotal(changes);
        return this.updateBuyCount(userCount);
    }

    @Override
    public Integer updateBuyCount(UserCount userCount) {
        if (userCount.getUpdateTime() == null) {
            userCount.setUpdateTime(DateUtil.getNow());
        }
        return userCountDao.updateBuyCount(userCount);
    }

    @Override
    public Integer updateSaleCount(Integer userId, Integer changes) {
        UserCount userCount = new UserCount();
        userCount.setUserId(userId);
        userCount.setSaleTotal(changes);
        return this.updateSaleCount(userCount);
    }

    @Override
    public Integer updateSaleCount(UserCount userCount) {
        if (userCount.getUpdateTime() == null) {
            userCount.setUpdateTime(DateUtil.getNow());
        }
        return userCountDao.updateSaleCount(userCount);
    }

    @Override
    public Integer updatePointCount(Integer userId, Integer changes) {
        UserCount userCount = new UserCount();
        userCount.setUserId(userId);
        userCount.setPoint(changes);
        return this.updatePointCount(userCount);
    }

    @Override
    public Integer updatePointCount(UserCount userCount) {
        if (userCount.getUpdateTime() == null) {
            userCount.setUpdateTime(DateUtil.getNow());
        }
        return userCountDao.updatePointCount(userCount);
    }

    @Override
    public Integer updateGrowthCount(Integer userId, Integer changes) {
        UserCount userCount = new UserCount();
        userCount.setUserId(userId);
        userCount.setGrowth(changes);
        return this.updateGrowthCount(userCount);
    }

    @Override
    public Integer updateGrowthCount(UserCount userCount) {
        if (userCount.getUpdateTime() == null) {
            userCount.setUpdateTime(DateUtil.getNow());
        }
        return userCountDao.updateGrowthCount(userCount);
    }

    @Override
    public Integer updateBalance(Integer userId, Integer changes) {
        UserCount userCount = new UserCount();
        userCount.setUserId(userId);
        userCount.setBalance(changes);
        return this.updateBalance(userCount);
    }

    @Override
    public Integer updateBalance(UserCount userCount) {
        if (userCount.getUpdateTime() == null) {
            userCount.setUpdateTime(DateUtil.getNow());
        }
        return userCountDao.updateBalance(userCount);
    }

    @Override
    public Integer updateRecharge(Integer userId, Integer changes) {
        UserCount userCount = new UserCount();
        userCount.setUserId(userId);
        userCount.setRecharge(changes);
        return this.updateRecharge(userCount);
    }

    @Override
    public Integer updateRecharge(UserCount userCount) {
        if (userCount.getUpdateTime() == null) {
            userCount.setUpdateTime(DateUtil.getNow());
        }
        return userCountDao.updateRecharge(userCount);
    }

    @Override
    public Integer insert(UserCount userCount) {
        // userCount.setLastUpgradeTime(null);
        if (userCount.getUpdateTime() == null) {
            userCount.setUpdateTime(DateUtil.getNow());
        }
        return userCountDao.insert(userCount);
    }

    @Override
    public Integer deleteByKey(Integer userId) {
        return userCountDao.deleteByKey(userId);
    }
}
